#!/usr/bin/perl -w
use strict;

my $x1 = $ARGV[0];  #put name of file as command line argument
my $genetic_file=$ARGV[1];
my $MB_number;
my $gene;
my $type_of_mut;
my $flag = 0; 

my %genetic_code; 
open INFILE1, $genetic_file or die "couldn't open $genetic_file\n";
while (<INFILE1>)
       	{
	chomp;
	my @info = split("\t", $_);
	$genetic_code{$info[1]} = $info[0]; 
	}
close INFILE1;
	
	
while ( my ($key, $value) = each(%genetic_code) ) 
	{
  	#print "$key => $value\n";
    	}	
#die;
my @AoA;
open INFILE1, "$x1" or die "couldn't open $x1\n";
while (<INFILE1>)
       	{
       	chomp;
	my @info = split("\t", $_);
	push @AoA, [ @info ]; 
	}

for my $i ( 0 .. $#AoA ) 
	{
	for my $j ( 0 .. $#{ $AoA[$i] } ) 
		{
        	#print "elt $i $j is $AoA[$i][$j]\n";
     		}
 	}
my %genetic_code2 = %genetic_code;
my %genetic_code3 = %genetic_code;
#die;
my $final_amino;
my $hold_dna1;
my $hold_dna2;
for my $i ( 0 .. $#AoA ) 
	{
	my $hold = $i + 1; 
	#my $hold = $AoA[$i][1] + 1; 
	if ($AoA[$i][9] && $AoA[$hold][9])
		{
		if ($AoA[$i][9] =~ /:p.(\D\d+)(\D)/)
			{
			my $one = $1; 
			my $change = $2;
			if ($AoA[$hold][9])
				{
				if ($AoA[$hold][9] =~ /:p.((\D)\d+)(\D)/)
					{
					my $two = $1; 
					my $change2 = $3; 
					my $start_amino = $2;
					#print "SECOND CHANGE\t$change2\n";
					if ("$one" eq "$two")
						{
						my ($k17, $k19, $k25, $k37, $k36, $k29);
						if($AoA[$i][17])
							{
							if($AoA[$i][17]eq"D")
								{
								$k17="D";
								}
							elsif($AoA[$hold][17] && $AoA[$hold][17] eq "D")
								{
								$k17="D";
								}
							else
								{
								$k17 = " ";
								}
							}
						if($AoA[$i][19])
							{
							if($AoA[$i][19] eq "D")
								{
								$k19 = "D";
								}
							elsif($AoA[$hold][19] && $AoA[$hold][19] eq "D")
								{
								$k19 = "D";
								}
							else
								{
								$k19 = " ";
								}
							}
						if($AoA[$i][29])
							{
							if($AoA[$i][29] eq "D")
								{
								$k29 = "D";
								}
							elsif($AoA[$hold][29] && $AoA[$hold][29] eq "D")
								{
								$k29 = "D";
								}
							else
								{
								$k29 = " ";
								}
							}
						if($AoA[$i][25])
							{
							if($AoA[$hold][25])
								{
								if($AoA[$i][25] eq "D"|| $AoA[$i][25] eq "A")
									{
									$k25 = "D";
									}
								elsif($AoA[$hold][25] eq "D" || $AoA[$hold][25] eq "A")
									{
									$k25 = "D";
									}
								}				
							else
								{
								$k25 = ".";
								}
							}
						if($AoA[$i][36])
							{
							if($AoA[$i][36] >= "15")
								{
								$k36 = $AoA[$i][36];
								}
							elsif($AoA[$hold][36] && $AoA[$hold][36] >= "15")
								{
								$k36 = $AoA[$hold][36];
								}
							else
								{
								$k36 = "0";
								}
							}
						if($AoA[$i][37] && $AoA[$hold][37])
							{
							if($AoA[$i][37] =~ /d+/ && $AoA[$i][37] >= "2")
								{
								$k37 = $AoA[$i][37];
								}
							elsif($AoA[$hold][37] =~ /d+/ && $AoA[$hold][37] >= "2")
								{
								$k37 = $AoA[$hold][37];
								}
							else
								{
								$k37 = "0";
								}
							}

						while ( my ($key2, $value2) = each(%genetic_code2) ) 
							{
							if ("$value2" eq "$start_amino")
								{
								#print "\t$value2\t$start_amino\t$key2\n";
								my $found_flag = 0;
								my $store_start = " ";
								my $store_new1 = " ";
								my $store_new2 = " ";
								while ( my ($key, $value) = each(%genetic_code) ) 
									{
									if ("$value" eq "$change")
										{
										#print "CHANGE1\t$value\t$change\t$key\n"; 
										my @info = split("", $key);
										my @info2 = split("", $key2);
										my $match1 = 0;
										my $match2 = 0;
										my $match3 = 0;
										if ("$info[0]" eq "$info2[0]") 
											{
											#print "$info[0] eq $info2[0]\n";
											$match1 = 1; 
											}
										if ("$info[1]" eq "$info2[1]") 
											{
											#print "$info[1] eq $info2[1]\n";
											$match2 = 1; 
											}
										if ("$info[2]" eq "$info2[2]") 
											{
											#print "$info[2] eq $info2[2]\n";
											$match3 = 1; 
											}
										my $match_total = $match1 + $match2 + $match3;
										if($match_total == 2)
											{
											$store_start =$key2; 
											$store_new1 =$key; 
											#print "WE NEED A NEW AMINO ACID $store_start to $store_new1\n"; 
											$found_flag = 1;
											}
										}
									}
								while ( my ($key, $value) = each(%genetic_code) ) 
									{
									if ("$value" eq "$change2")
										{
										#print "CHANGE2\tchange\t$value\t$change\t$key\n"; 
										my @info = split("", $key);
										my @info2 = split("", $key2);
										my $match1 = 0;
										my $match2 = 0;
										my $match3 = 0;
										if ("$info[0]" eq "$info2[0]") 
											{
										#	print "$info[0] eq $info2[0]\n";
											$match1 = 1; 
											}
										if ("$info[1]" eq "$info2[1]") 
											{
										#	print "$info[1] eq $info2[1]\n";
											$match2 = 1; 
											}
										if ("$info[2]" eq "$info2[2]") 
											{
										#	print "$info[2] eq $info2[2]\n";
											$match3 = 1; 
											}
										my $match_total = $match1 + $match2 + $match3;
										if($match_total == 2)
											{
											$store_new2 =$key;
										#	print "WE NEED Another NEW AMINO ACID $store_start to $store_new2 \n"; 
											$found_flag = 2;
											}
										#print "change2\t$found_flag\t$store_new2\t$store_new1\t$value\t$change2\t$key\n"; 
										}
									}								
								my $store_changed = "";
								if ($found_flag ==2)
									{
									if($store_start && $store_new1 && $store_new2)
										{	
										my @info = split("", $store_start);
										my @info2 = split("", $store_new1);
										my @info3 = split("", $store_new2);
										if ("$info[0]" eq "$info2[0]" && "$info[0]" eq "$info3[0]")
											{
											$store_changed=$info[0];
											} 
										elsif("$info[0]" eq "$info2[0]" && "$info[0]" ne "$info3[0]")
											{
											$store_changed=$info3[0];$hold_dna1=$info[0];
											$hold_dna2=$info3[0];
											}
										elsif("$info[0]" ne "$info2[0]" && "$info[0]" eq "$info3[0]")
											{
											$store_changed=$info2[0];
											}
										if ($info[1] && $info2[1])
											{
											if ("$info[1]" eq "$info2[1]" && "$info[1]" eq "$info3[1]")
												{
												$store_changed="$store_changed"."$info[1]";
												} 
											elsif("$info[1]" eq "$info2[1]" && "$info[1]" ne "$info3[1]")
												{
												$store_changed="$store_changed"."$info3[1]";
												$hold_dna1=$info[1];$hold_dna2=$info3[1];
												}
											elsif("$info[1]" ne "$info2[1]" && "$info[1]" eq "$info3[1]")
												{
												$store_changed="$store_changed"."$info2[1]";
												}
											}
										if ($info[2] && $info2[2])
											{
											if ("$info[2]" eq "$info2[2]" && "$info[2]" eq "$info3[2]")
												{
												$store_changed="$store_changed"."$info[2]";
												} 
											elsif("$info[2]" eq "$info2[2]" && "$info[2]" ne "$info3[2]")
												{
												$store_changed="$store_changed"."$info3[2]";
												$hold_dna1=$info[2];$hold_dna2=$info3[2];
												}
											elsif("$info[2]" ne "$info2[2]" && "$info[2]" eq "$info3[2]")
												{
												$store_changed="$store_changed"."$info2[2]";
												}
											}
										#print "$store_start changed to $store_changed\n$store_new1\n$store_new2\n";  
										}
									 while ( my ($key3, $value3) = each(%genetic_code3) )
                                                				{
                                                				if ("$key3" eq "$store_changed")	
											{
											$final_amino = $value3;
											#print "Dinucleotide FOUND $store_changed\t$key3\t NOW $value3\n";
											}
										
										}
									}
								}
							}
						for my $j ( 0 .. $#{ $AoA[$i] } ) 
							{
							if ("$j" == "3") 	
								{
								print "$AoA[$i][$j]$hold_dna1\t";
								}
							elsif ("$j" == "4") 	
								{
								print "$AoA[$i][$j]$hold_dna2\t";
								}
							elsif ("$j" == "9") 	
								{
								if ($AoA[$i][9] =~/,/)
									{
									my @info = split(",", $AoA[$i][9]);
									my $number = scalar @info; 
									#print "NUMBER of INFO $number\t";
									my $count = 0;
									foreach(@info)
										{
										if ($_ =~ m/(\S+:p.)(\D\d+)(\D)/g)
											{
											print "$1$2$final_amino";
											}
										$count++;
										if($count == $number)
											{print "\t";}
										elsif($count != $number)
											{print ",";}
										}
									#print "\t";
									}
								elsif($AoA[$i][9] =~ m/(\S+:p.)(\D\d+)(\D)/g) 
									{
									print "$1$2$final_amino\t";
    									#print "$1\n";
									}
								#if ($AoA[$i][9] =~ /(.*:p.)(\D\d+)(\D)/)
								#	{
								#	}
								}
							elsif ("$j" == "14" && $AoA[$i][14]) 	
								{
								if ($AoA[$i][14] =~/,/)
									{
									my @info = split(",", $AoA[$i][14]);
									my $number = scalar @info; 
									#print "NUMBER of INFO $number\t";
									my $count = 0;
									foreach(@info)
										{
										if ($_ =~ m/(\S+:p.)(\D\d+)(\D)/g)
											{
											print "$1$2$final_amino";
											}
										$count++;
										if($count == $number)
											{print "\t";}
										elsif($count != $number)
											{print ",";}
										}
									#print "\t";
									}
								elsif($AoA[$i][14] =~ m/(\S+:p.)(\D\d+)(\D)/g) 
									{
									print "$1$2$final_amino\t";
    									#print "$1\n";
									}
								}
							elsif ($AoA[$i][17] && $k17 && "$j" == "17") {print "$k17\t";}
							elsif ($AoA[$i][19] && $k19 && "$j" == "19") {print "$k19\t";}
							elsif ($AoA[$i][25] && $k25 && "$j" == "25") {print "$k25\t";}
							elsif ($AoA[$i][29] && $k29 && "$j" == "29") {print "$k29\t";}
							elsif ($AoA[$i][36] && $k36 && "$j" == "36") {print "$k36\t";}
							elsif ($AoA[$i][37] && $k37 && "$j" == "37") {print "$k37\t";}

							else
								{
								print "$AoA[$i][$j]\t";
								}
     							}
						
						print "\n";
					
						#print "$one\t$two\n";
						for my $j ( 0 .. $#{ $AoA[$hold] } ) 
							{
							$AoA[$hold][$j] = "";
     							}

					}
					else
						{
						for my $j ( 0 .. $#{ $AoA[$i] } ) 
							{
							print "$AoA[$i][$j]\t";
     							}
							print "\n";
						}	

				}
				else
					{
					for my $j ( 0 .. $#{ $AoA[$i] } ) 
						{
						print "$AoA[$i][$j]\t";
     						}
						print "\n";
					}	
			}
			else
				{
				for my $j ( 0 .. $#{ $AoA[$i] } ) 
					{
					print "$AoA[$i][$j]\t";
     					}
				print "\n";
				}	
			}
		}
	else
		{
		for my $j ( 0 .. $#{ $AoA[$i] } ) 
			{
			print "$AoA[$i][$j]\t";
     			}
		print "\n";
		}	
 	}
=comment
								}
							}
					}}}}}}
die;


						
=cut
